package com.community.zhihuishequ.net;

import android.content.Context;

import java.io.IOException;
import java.util.Random;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * App的拦截器
 */
public class AppInterceptor implements Interceptor {

    private String sessionId;
    private String timestamp;
    private String token;
    private String nonce;
    private String signature;
    private Context context;

    public AppInterceptor(Context context) {
//        LoginCacheDb db = new LoginCacheDb();
//        sessionId = db.getSessionId();
//        timestamp = String.valueOf(System.currentTimeMillis());
//        token = db.getToken();
//        nonce = String.valueOf(100000 + new Random().nextInt(1000000 - 100000));
//        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        builder.addHeader("sessionId", sessionId);
        builder.addHeader("timestamp", timestamp);
        builder.addHeader("nonce", nonce);
        String urlPart = request.url().toString().replace(AppRestService.BASE_URL, "/");
//        signature = SignatureUtil.genSignature(urlPart, token, timestamp, nonce);
        if (signature != null) {
            builder.addHeader("signature", signature);
        }
        return chain.proceed(builder.build());
    }
}
